import { createStore } from "vuex";
import persistedState from "vuex-persistedState";

const store = createStore({
  plugins: [persistedState()],
  state: {
    searchList: [],
    HistorySearch: ["手机", "电脑", "口红", "笔记本", "VR眼镜"],
    username: "",
    password: "",
    token: "",
    cartLength: 0,
    userid: "",
  },

  mutations: {
    removeUserinfo(state) {
      state.username = "";
      state.password = "";
      state.token = "";
      state.userid = "";
    },

    setHistorySearch(state, value) {
      if (state.HistorySearch.indexOf(value) === -1) {
        state.HistorySearch.unshift(value);
      }
    },
    setSearchList(state, list) {
      state.searchList = list;
    },
    settoken(state, token) {
      state.token = token;
    },
    setuserid(state, id) {
      state.userid = id;
    },
    setUsername(state, user) {
      state.username = user;
    },
    setPassword(state, password) {
      state.password = password;
    },
    setCartLength(state, num) {
      state.cartLength = num;
    },
  },
});
export default store;
